
using System.Web.UI.HtmlControls;
using System;
using System.Configuration;
using System.Drawing;
using System.Data;
using System.Web.UI.WebControls;
using Microsoft.VisualBasic;
using System.Collections;
using System.Web.UI;
using System.Web;


using System.IO;
using System.Text;
//using com.ddsc.database;
using DDSC.OracleADO;
using com.ddsc.common;
using SMAS;
using System.Resources;


namespace smasb2b
{
	public class EMPD0010 : BasePage
	{
		
#region  Web Form 設計工具產生的程式碼
		
		//此為 Web Form 設計工具所需的呼叫。
		[System.Diagnostics.DebuggerStepThrough()]private void InitializeComponent()
		{
			
		}
		protected System.Web.UI.WebControls.Label lblQUERYINVENTORY;
		protected System.Web.UI.WebControls.Label lblDEPT_DATE;
		protected System.Web.UI.WebControls.Label lblCDOE;
		protected System.Web.UI.WebControls.Label lblTO2;
		protected System.Web.UI.WebControls.Label lblTO1;
		
		
		protected System.Web.UI.WebControls.Label lblDES;
		protected System.Web.UI.HtmlControls.HtmlTable Table3;
		protected System.Web.UI.HtmlControls.HtmlTableRow DataGrid_foot;
		protected System.Web.UI.WebControls.Label lblCODE;
		protected System.Web.UI.WebControls.Label lblITEM_NAME;
		protected System.Web.UI.WebControls.Label lblSALE_QTY;
		protected System.Web.UI.WebControls.Label lblINVENTORY_QTY;
		protected System.Web.UI.HtmlControls.HtmlInputImage Image1;
		protected System.Web.UI.WebControls.Label lblMATERIALQUERY;
		protected System.Web.UI.WebControls.Label lblENGLISH_SHORTNAME;
		protected System.Web.UI.WebControls.Label lblCLASS;
		protected System.Web.UI.WebControls.Label lblSTATUS;
		protected ddscControlLibrary.InputBoxCtrl PageIndex;
		protected ddscControlLibrary.InputBoxCtrl PageCount;
		protected ddscControlLibrary.InputBoxCtrl GotoPage;
		protected System.Web.UI.HtmlControls.HtmlTable Table1;
		protected System.Web.UI.HtmlControls.HtmlTable DataGrid;
		protected ddscControlLibrary.InputBoxCtrl txtCODE1;
		protected ddscControlLibrary.InputBoxCtrl txtCODE2;
		protected System.Web.UI.WebControls.Label lblCODE1;
		protected ddscControlLibrary.DateCtrl txtDEPT_DATE1;
		protected ddscControlLibrary.DateCtrl txtDEPT_DATE2;
		protected System.Web.UI.WebControls.Label lblMSG012;
		protected System.Web.UI.HtmlControls.HtmlImage IMG1;
		protected System.Web.UI.WebControls.ImageButton ImageButton1;
		protected System.Web.UI.WebControls.ImageButton BtnNext;
		protected System.Web.UI.WebControls.ImageButton btnPREV;
		protected System.Web.UI.WebControls.ImageButton BtnGo;
		protected ddscControlLibrary.SubmitButtonCtrl btnSearch;
		protected System.Web.UI.WebControls.Label lblCODE_ERR;
		protected System.Web.UI.WebControls.Label lblDEPT_DATE_ERR;
		protected System.Web.UI.WebControls.Label lblPAGEINDEX;
		protected System.Web.UI.WebControls.Label lblTOTAL;
		protected System.Web.UI.WebControls.Label lblPAGE1;
		protected System.Web.UI.WebControls.Label lblPAGE;
		protected System.Web.UI.WebControls.Label lblCODE2;
		protected System.Web.UI.WebControls.Label lblCODE3;
		protected System.Web.UI.WebControls.Label lblJUNK_QTY;
		protected System.Web.UI.WebControls.Label lblEXP_INV_QTY;
		protected System.Web.UI.WebControls.Label lblEXP_JUNK_QTY;
		protected System.Web.UI.WebControls.Label lblPRINTPAGE;
		protected System.Web.UI.HtmlControls.HtmlInputHidden hidVENDOR;
		protected ddscControlLibrary.InputBoxCtrl txtEmail;
		
		//注意: 下列預留位置宣告是 Web Form 設計工具需要的項目。
		//請勿刪除或移動它。
		private System.Object designerPlaceholderDeclaration;
		
		
		private void Page_Init(System.Object sender, System.EventArgs e)
		{
			//CODEGEN: 此為 Web Form 設計工具所需的方法呼叫
			//請勿使用程式碼編輯器進行修改。
			InitializeComponent();
		}
		
#endregion
		BasePage BasePage = new BasePage();
		
		private void Page_Load(System.Object sender, System.EventArgs e)
		{
			//在這裡放置使用者程式碼以初始化網頁
			if (IsReSubmit)
			{
				DataGrid_DataBind();
			}
			
			this.BtnGo.Enabled = false;
			this.BtnNext.Enabled = false;
			this.btnPREV.Enabled = false;
			this.btnSearch.Click+=btnSearch_Click;
			VENDOR loginUser = GetLoginUser();
			if (!(loginUser == null))
			{
				hidVENDOR.Value = loginUser.VENDOR_NO;
			}
			
		}
		//Private Sub btnFIRST_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFIRST.Click
		//    PageIndex.Text = 1
		//    DataGrid_DataBind()
		//End Sub
		
		//Private Sub btnPREV_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPREV.Click
		//    PageIndex.Text = CStr(CInt(PageIndex.Text) - 1)
		//    DataGrid_DataBind()
		//End Sub
		
		//Private Sub btnNEXT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNEXT.Click
		//    PageIndex.Text = CStr(CInt(PageIndex.Text) + 1)
		//    DataGrid_DataBind()
		//End Sub
		
		//Private Sub btnLAST_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLAST.Click
		//    PageIndex.Text = CStr(PageCount.Text)
		//    DataGrid_DataBind()
		//End Sub
		
		//Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
		//    PageIndex.Text = CStr(GotoPage.Text)
		//    DataGrid_DataBind()
		//End Sub
		
		public void DataGrid_DataBind()
		{
			//變數宣告
			VENDOR LoginUser = GetLoginUser();
			DataSet ds = default(DataSet);
			dbAccess dba = new dbAccess();
            ArrayList m_arrParam = new ArrayList();
			string strCondition = "";
			int i;
			int count = 1;
			string[] str_SQL_a = new string[count + 1];
			string[] str_SQL_b = new string[count + 1];
			
			try
			{
				//目前頁次指定Goto
				GotoPage.Text = Server.HtmlEncode(PageIndex.Text);
				
				//預設查詢語法
				
				//Dim SqlCommandText = "SELECT A.CODE,B.ITEM_NAME,A.SALE_QTY,B.INVENTORY_QTY FROM " _
				//                   + " (SELECT A.CODE,NVL(SUM(A.SALE_QTY_UPD),0)SALE_QTY  " _
				//                   + " FROM VND_INVENTORY_SUMMARY A WHERE  A.DEPT_DATE BETWEEN '" & Me.txtDEPT_DATE1.Text & "' AND '" & Me.txtDEPT_DATE2.Text & "' GROUP BY A.CODE)A  " _
				//                   + " ,(SELECT A.CODE,A.ITEM_NAME,B.INVENTORY_QTY FROM  " _
				//                   + " (SELECT A.CODE,DECODE(B.ENGLISH_SHORTNAME,null,B.CHINESE_SHORTNAME,B.ENGLISH_SHORTNAME) ITEM_NAME FROM " _
				//                   + " (SELECT CODE FROM VND_SUPPLY WHERE VENDOR_NO='" & LoginUser.VENDOR_NO & "')A,BAS_MATERIAL B WHERE " _
				//                   + " A.CODE=B.CODE(+) and b.material_type='S'  " _
				//                   + " and b.code not in (select code from BAS_MATERIAL where VENDOR_NO='" & LoginUser.VENDOR_NO & "'and  class='G' and subclass='7'))A, " _
				//                   + " (SELECT CODE,NVL(SUM(ABS(CURR_QTY)),0) AS INVENTORY_QTY FROM INV_INVENTORY where store_no in(select store_no from BAS_STORE where store_type='1' and sign_flag='Y') GROUP BY CODE)B " _
				//                   + " WHERE A.CODE=B.CODE(+))B WHERE B.CODE=A.CODE(+) AND A.CODE IS NOT NULL "
				
				//string SqlCommandText = "SELECT m.order_no, SUM(order_amt) order_amt FROM emp_order_master m, emp_order_details d WHERE m.order_no = d.order_no AND m.CUST_EMAIL = \'" + txtEmail.Text.Trim().Replace("\'", string.Empty) + "\' GROUP BY m.ORDER_NO ORDER BY m.ORDER_NO";

                string SqlCommandText = "SELECT m.order_no, SUM(order_amt) order_amt FROM emp_order_master m, emp_order_details d WHERE m.order_no = d.order_no AND m.CUST_EMAIL = :EMAIL GROUP BY m.ORDER_NO ORDER BY m.ORDER_NO";
				
				
				//Dim SqlCommandCount = "SELECT COUNT (A.CODE) as row_total_count  FROM " _
				//                   + " (SELECT A.CODE,NVL(SUM(A.SALE_QTY_UPD),0)SALE_QTY  " _
				//                   + " FROM VND_INVENTORY_SUMMARY A WHERE  A.DEPT_DATE BETWEEN '" & Me.txtDEPT_DATE1.Text & "' AND '" & Me.txtDEPT_DATE2.Text & "' GROUP BY A.CODE)A  " _
				//                   + " ,(SELECT A.CODE,A.ITEM_NAME,B.INVENTORY_QTY FROM  " _
				//                   + " (SELECT A.CODE,DECODE(B.ENGLISH_SHORTNAME,null,B.CHINESE_SHORTNAME,B.ENGLISH_SHORTNAME) ITEM_NAME FROM " _
				//                   + " (SELECT CODE FROM VND_SUPPLY WHERE VENDOR_NO='" & LoginUser.VENDOR_NO & "')A,BAS_MATERIAL B WHERE " _
				//                   + " A.CODE=B.CODE(+) and b.material_type='S'  " _
				//                   + " and b.code not in (select code from BAS_MATERIAL where VENDOR_NO='" & LoginUser.VENDOR_NO & "'and  class='G' and subclass='7'))A, " _
				//                   + " (SELECT CODE,NVL(SUM(ABS(CURR_QTY)),0) AS INVENTORY_QTY FROM INV_INVENTORY where store_no in(select store_no from BAS_STORE where store_type='1' and sign_flag='Y') GROUP BY CODE)B " _
				//                   + " WHERE A.CODE=B.CODE(+))B WHERE B.CODE=A.CODE(+) AND A.CODE IS NOT NULL "

                //string SqlCommandCount = "SELECT COUNT(*) row_total_count FROM emp_order_master WHERE CUST_EMAIL = \'" + txtEmail.Text.Trim().Replace("\'", string.Empty) + "\' ";
                string SqlCommandCount = "SELECT COUNT(*) row_total_count FROM emp_order_master WHERE CUST_EMAIL = :EMAIL ";
				
				if (!String.IsNullOrEmpty(strCondition))
				{
                    strCondition = " and  " + strCondition.Substring(0, strCondition.Length - 5);
					// strCondition = " where VENDOR_NO='" & LoginUser.VENDOR_NO & "'" + strCondition.Substring(0, strCondition.Length - 3)
					
				}
				
				int intCount = 0;
				int size = PageSize; // Update by titan 由BasePage設定
                //Ken : 2.設定動態參數物件
                m_arrParam.Add(dba.GetOracleParam(":EMAIL", txtEmail.Text.Trim().Replace("\'", string.Empty) ));

                // 計算資料總筆數
				//DataSet dsCount = dba.runExecuteQuery("Vendor_Count", SqlCommandCount + strCondition);
                //Ken : 3.修改執行方法Overload
                DataSet dsCount = dba.runExecuteQuery("Vendor_Count",SqlCommandCount + strCondition, m_arrParam);
				if (dsCount.Tables[0].Rows[0]["ReturnCode"] == "000")
				{
					intCount = int.Parse(System.Convert.ToString(dsCount.Tables["Vendor_Count"].Rows[0]["row_total_count"]));
					PageCount.Text = System.Convert.ToString(Math.Ceiling(System.Convert.ToDecimal(double.Parse((intCount).ToString()) / size)));
				}
				
				//Update By Titan 處理當總頁次小於目前頁次
				if ((int.Parse(PageCount.Text) < int.Parse(PageIndex.Text)) && int.Parse(PageCount.Text) > 0)
				{
					// 總筆數(intCount)小於一頁，從第0筆開始顯示
					PageIndex.Text = Server.HtmlEncode(PageCount.Text);
					GotoPage.Text = Server.HtmlEncode(PageCount.Text);
				}
				
				// Update by titan 由BasePage設定
				// absolute position
				int position = ((int.Parse(PageIndex.Text)) - 1) * size;
				int maxrecord = position + size;
				
				
				//控制分頁按鈕
				ControlPageButton(DataGrid_foot);
				
				//執行資料庫語法
				//ds = dba.runExecuteQuery("VND_SUPPLY", SqlCommandText, size, position);
                ds = dba.runExecuteQuery("VND_SUPPLY", SqlCommandText, position,size, m_arrParam);
				
				if (!(ds == null) && ds.Tables["info"].Rows[0]["ReturnCode"] != "100" && ds.Tables["info"].Rows[0]["ReturnCode"] != "999")
				{
					ArrayList ArrKey = new ArrayList();
					bindGrid(DataGrid, ArrKey, ds.Tables["VND_SUPPLY"]);
				}
				else
				{
					showException("查無搜尋資料!");
				}
				
			}
			catch (Exception ex)
			{
				throw (ex);
			}
			finally
			{
				ds.Dispose();
			}
		}
		
		private void bindGrid(HtmlTable DataGrid, ArrayList ArrKey, DataTable DataSource)
		{
			HtmlTableRow tr = (HtmlTableRow) (DataGrid.FindControl("DataGrid_head"));
			int i = 0;
			int j = 0;
			int k;
			string Url;
			string className = "";
			ResourceManager rm = GetResourceManager();
			FunctionHandler fn = new FunctionHandler();
			
			for (i = 0; i <= DataSource.Rows.Count - 1; i++)
			{
				HtmlTableRow htr = new HtmlTableRow();
				if (i % 2 == 0)
				{
					className = "evenRow";
				}
				else
				{
					className = "oddRow";
				}
				htr.Attributes.Add("class", className);
				DataGrid.Rows.Add(htr);
				for (j = 0; j <= tr.Cells.Count - 1; j++)
				{
					HtmlTableCell hd = new HtmlTableCell();
					hd.Width = tr.Cells[j].Width;
					hd.Align = tr.Cells[j].Align;
					
					switch (j)
					{
						case 0:
							string strData_1 = System.Convert.ToString(DataSource.Rows[i]["ORDER_NO"].ToString());
							hd.InnerHtml = string.Format("<a href=\"EMPD0012.aspx?o={1}\" target=\"_blank\">{0}</a>", strData_1, Server.UrlEncode(fn.Encrypt(strData_1)));
							break;
						case 1:
							string strData = System.Convert.ToString(DataSource.Rows[i]["ORDER_AMT"].ToString());
							hd.InnerHtml = strData;
							break;
					}
					DataGrid.Rows[DataGrid.Rows.Count - 1].Cells.Add(hd);
				}
			}
		}
		
		
		
		private void BtnNext_Click(System.Object sender, System.Web.UI.ImageClickEventArgs e)
		{
			try
			{
				PageIndex.Text = ((int.Parse(PageIndex.Text)) + 1).ToString();
				DataGrid_DataBind();
			}
			catch (Exception ex)
			{
				showException(ex.Message);
			}
		}
		
		private void btnPREV_Click(System.Object sender, System.Web.UI.ImageClickEventArgs e)
		{
			try
			{
				PageIndex.Text = ((int.Parse(PageIndex.Text)) - 1).ToString();
				DataGrid_DataBind();
			}
			catch (Exception ex)
			{
				showException(ex.Message);
			}
		}
		
		private void BtnGo_Click(System.Object sender, System.Web.UI.ImageClickEventArgs e)
		{
			ResourceManager rm = GetResourceManager();
			try
			{
				PageIndex.Text = Server.HtmlEncode(GotoPage.Text);
				DataGrid_DataBind();
			}
			catch (Exception)
			{
				showException(rm.GetString("MSG057"));
			}
		}
		
		private void btnSearch_Click(System.Object sender, System.EventArgs e)
		{
			try
			{
				PageIndex.Text = System.Convert.ToString(1);
				DataGrid_DataBind();
			}
			catch (Exception ex)
			{
				showException(ex.Message);
			}
		}
	}
	
}
